//
//  ChooseMoneyView.m
//  ZhuanZuBao
//
//  Created by bchy on 2020/9/14.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "ChooseMoneyView.h"

@interface ChooseMoneyView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation ChooseMoneyView

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        NSArray *arr = @[
            @"不限",
            @"500元以下",
            @"500-1000元",
            @"1000-1500元",
            @"1500-2000元",
            @"2000-3000元",
            @"3000-4500元",
            @"4500元以上"
        ];
        [_dataArr addObjectsFromArray:arr];
    }
    return _dataArr;
}

-(instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.4];
    
    _tableView = [UITableView new];
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorColor = SEPARATECOLOR;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.height.mas_equalTo(380);
    }];
    
    UIButton *cancelBtn = [UIButton new];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.top.equalTo(self.tableView.mas_bottom);
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    if (_selectedIndex == indexPath.row) {
        cell.textLabel.textColor = THEMECOLOR;
    }else{
        cell.textLabel.textColor = BLACKTEXTCOLOR;
    }
    cell.textLabel.text = _dataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selectedIndex = indexPath.row;
    [self.tableView reloadData];
    NSInteger min = 0;
    NSInteger max = 0;
    switch (indexPath.row) {
        case 1:
            min = 0;
            max = 500;
            break;
        case 2:
            min = 500;
            max = 1000;
            break;
        case 3:
            min = 1000;
            max = 1500;
            break;
        case 4:
            min = 1500;
            max = 2000;
            break;
        case 5:
            min = 2000;
            max = 3000;
            break;
        case 6:
            min = 3000;
            max = 4500;
            break;
        case 7:
            min = 4500;
            max = 20000;
            break;
        default:
            break;
    }
    if (self.selectMoneyBlock) {
        self.selectMoneyBlock(min, max);
    }
    self.hidden = YES;
}

-(void)cancel{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    self.hidden = YES;
}
@end
